<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><spring:message code="message.lable.proddruginfoSTDmerge"/></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp" %> 
    <style type="text/css">
        div.leftDiv {
            width: 49%;
            float: left;
        }
        div.rightDiv {
            width: 49%;
            float: right;
        }
    </style>
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content" style="margin-top:10px;">
        <div class="leftDiv">
	        <div class=" box box-success">
	            <div class="box-body">
	                <div class="input">
                		<div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
	                        <div class="boxinput">
	                        <input type="text" class="textinput" id="qualityspecName" name="qualityspecName" type="text" placeholder="<spring:message code="message.listForm.tongYongMing"/>" />
		                    </div>
	                    <div class="textalign1"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
	                        <div class="boxinput">
	                        <input type="text" class="textinput" id="drugcode" name="drugcode" type="text" placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
	                    	</div>
	                    </div>
	                     <div class="input" style="margin-top:3px">
	                    <div class="textalign1"><spring:message code="message.listForm.shengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
	                        <div class="boxinput">
	                        <input type="text" class="textinput"  id="prodCompName" name="prodCompName" type="text" placeholder="<spring:message code="message.listForm.shengChanQiYe"/>" />
	                  		</div>
	                  	<div class="textalign1" >
	                    <button id="selectDrug" type="button" onclick="gridSerch();" class="btn btn-success btn-sm"><spring:message code="message.button.seachSpacing"/></button>
	                    </div>
	                </div>
	            </div>
	        </div>
	        <table class="jqgrid" id="gridlist"></table>
	        <div id="gridpage"></div>
        </div>
        
	    <div class="rightDiv">
	        <table class="jqgrid" id="mergerGridList"></table>
	        <div style="height: 10px;"></div>
            <table class="jqgrid" id="mergedGridList"></table>
	    </div>
	    
	    <div style="clear: both;"></div>
    
	    <div class=" box box-warning bottongroup" style="margin-top:15px;">
	         <label onclick="saveMergeDrugInfo();" class="btn btn-default btn-sm"><spring:message code="message.button.combineSpacing"/></label>
	     </div>
     </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
     
    <script type="text/javascript">
    
  //绑定回车
    $('#drugcode,#qualityspecName,#prodCompName').bind('keydown', function(event) {
        if (event.keyCode == "13") {        //13 位电脑上回车键的位置代码
            $('#selectDrug').click();
        }
    }); 
  
    $("#qualityspecName").focus();//加载页面光标在第一个查询框内
    
        $(function () {
            
            $("#gridlist").jqGrid({
                url: "${ctx}/stdProdDrugInfo/getMergeProdDrugInfoList.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit : false,
                width: "100%",
                height: 270,
                colNames: [ 
                            'id',
                            '<spring:message code="message.listForm.yaoPinBianMa"/>', 
                            '<spring:message code="message.listForm.tongYongMing"/>', 
                            '<spring:message code="message.listForm.jiXing"/>', 
                            '<spring:message code="message.listForm.guiGe"/>', 
                            '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                            '<spring:message code="message.listForm.danWei"/>',
                            '<spring:message code="message.listForm.caiZhi"/>',
                            '<spring:message code="message.listForm.shengChanQiYe"/>',
                            '<spring:message code="message.lable.status"/>'
                            ],
                colModel: [
                    { name: 'prodId', width: 120 ,hidden:true, align:"center",sortable: false},
                    { name: 'drugCode', width: 120 , align:"center",sortable: false},
                    { name: 'qualityspecName', width: 180 , align:"left" ,sortable: false},
                    { name: 'formName', width: 80 , align:"center" ,sortable: false},
                    { name: 'specName', width: 80 , align:"left" ,sortable: false},
                    { name: 'factor', width: 80 , align:"center" ,sortable: false},
                    { name: 'unitName', width: 80 , align:"center" ,sortable: false},
                    { name: 'materialName', width: 80 , align:"center" ,sortable: false},
                    { name: 'prodCompName', width: 180 , align:"left",sortable: false },
                    { name: 'status', width: 80, align:"center",sortable: false,
                    	formatter:function(cellvalue, options, row){
                    		var str ="";
                    		if(cellvalue == 1){
                    			return str="<spring:message code="message.listForm.QiYong"/>";
                    		}else if(cellvalue == 2){
                    			return str="<spring:message code="message.listForm.TingYong"/>";
                    		}else if(cellvalue == "启用"){
                    			return str="<spring:message code="message.listForm.QiYong"/>";
                    		}
                    		return str;
                        } 
                    }
                ],
                rowNum: 20,
                rowList: [10,20,50,100],
                pager: "#gridpage",
                viewrecords: true,
                caption: "<spring:message code="message.list.proddruginfoStdlistDragging"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                },
                gridComplete:function(){        
                    var page = $("#gridlist").jqGrid('getGridParam','page');
                    var rows = $('#gridlist').getGridParam("rowNum");
                    var maxnum = (rows*page).toString();
                    var len = (maxnum.length)*10;
                    $('.jqgfirstrow').find('td').eq(0).css('width',len+'px');
                    $('#gridlist_rn').css('width',len+'px');
                }
            });
            
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            
            $("#mergerGridList").jqGrid({
            	datatype: "local",
                autowidth: true,
                shrinkToFit : false,
                width: "100%",
                height: 50,
                colNames: [
                           'id',
                           '<spring:message code="message.listForm.yaoPinBianMa"/>', 
                           '<spring:message code="message.listForm.tongYongMing"/>', 
                           '<spring:message code="message.listForm.jiXing"/>', 
                           '<spring:message code="message.listForm.guiGe"/>', 
                           '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                           '<spring:message code="message.listForm.danWei"/>',
                           '<spring:message code="message.listForm.caiZhi"/>',
                           '<spring:message code="message.listForm.shengChanQiYe"/>',
                           '<spring:message code="message.lable.status"/>'
                           ],
                colModel: [
                    { name: 'prodId', width: 120 ,hidden:true, align:"center",sortable: false},
                    { name: 'drugCode', width: 120 , align:"center",sortable: false},
                    { name: 'qualityspecName', width: 180 , align:"left" ,sortable: false},
                    { name: 'formName', width: 80 , align:"center" ,sortable: false},
                    { name: 'specName', width: 80 , align:"left" ,sortable: false},
                    { name: 'factor', width: 80 , align:"center" ,sortable: false},
                    { name: 'unitName', width: 80 , align:"center" ,sortable: false},
                    { name: 'materialName', width: 80 , align:"center" ,sortable: false},
                    { name: 'prodCompName', width: 180 , align:"left",sortable: false },
                    { name: 'status', width: 80 , align:"center",sortable: false }
                ],
                viewrecords: true,
                caption: "<spring:message code="message.list.proddruginfoStdlistSingle"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                },
                gridComplete:function(){        
                    var page = $("#mergerGridList").jqGrid('getGridParam','page');
                    var rows = $('#mergerGridList').getGridParam("rowNum");
                    var maxnum = (rows*page).toString();
                    var len = (maxnum.length)*10;
                    $('.jqgfirstrow').find('td').eq(0).css('width',len+'px');
                    $('#mergerGridList_rn').css('width',len+'px');
                }
            });
            
            $("#mergedGridList").jqGrid({
            	datatype: "local",
                autowidth: true,
                shrinkToFit : false,
                width: "100%",
                height: 270,
                colNames: [ 
                            'id',
                            '<spring:message code="message.listForm.yaoPinBianMa"/>', 
                            '<spring:message code="message.listForm.tongYongMing"/>', 
                            '<spring:message code="message.listForm.jiXing"/>', 
                            '<spring:message code="message.listForm.guiGe"/>', 
                            '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                            '<spring:message code="message.listForm.danWei"/>',
                            '<spring:message code="message.listForm.caiZhi"/>',
                            '<spring:message code="message.listForm.shengChanQiYe"/>',
                            '<spring:message code="message.lable.status"/>'
                            ],
                colModel: [
                    { name: 'prodId', width: 120 ,hidden:true, align:"center",sortable: false},
                    { name: 'drugCode', width: 120 , align:"center",sortable: false},
                    { name: 'qualityspecName', width: 180 , align:"left" ,sortable: false},
                    { name: 'formName', width: 80 , align:"center" ,sortable: false},
                    { name: 'specName', width: 80 , align:"left" ,sortable: false},
                    { name: 'factor', width: 80 , align:"center" ,sortable: false},
                    { name: 'unitName', width: 80 , align:"center" ,sortable: false},
                    { name: 'materialName', width: 80 , align:"center" ,sortable: false},
                    { name: 'prodCompName', width: 180 , align:"left",sortable: false },
                    { name: 'status', width: 80 , align:"center",sortable: false }
                ],
                viewrecords: true,
                caption: "<spring:message code="message.list.proddruginfoStdlistMore"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                },
                gridComplete:function(){        
                    var page = $("#mergedGridList").jqGrid('getGridParam','page');
                    var rows = $('#mergedGridList').getGridParam("rowNum");
                    var maxnum = (rows*page).toString();
                    var len = (maxnum.length)*10;
                    $('.jqgfirstrow').find('td').eq(0).css('width',len+'px');
                    $('#mergedGridList_rn').css('width',len+'px');
                }
            });
            
            $("#gridlist").jqGrid('gridDnD',{
            	connectWith : '#mergerGridList,#mergedGridList', 
            	"beforedrop" : function(self, ev, ui, getdata, $target, $self) {
            		var tableId = $target.getGridParam("id");
                    var ids = $target.jqGrid('getDataIDs');
                    var len = ids.length || 0;
                    var prodId = ui.prodId; //  当前拖动的id
                    var status = ui.status;
                    if (len > 0) {
                        if (ids[0] == "jqg_empty_row") {
                            len -= 1;
                        }
                    }
                    
                    if (tableId == "mergerGridList") {  //  主合并列表
                    	// 主合并列表只能放一条记录
                    	if (len >= 1) {
                    		ev.helper.dropped = false;
                    		$.alert("<spring:message code="message.yanZheng.ZhuHeBinZhiFangYiTiaoJiLu"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
                    		return;
                    	}
                        //  停用状态数据不能作为主合并列表数据
                    	if (status == 0) {
                            ev.helper.dropped = false;
                            $.alert("<spring:message code="message.yanZheng.TingYongStatusBuNengZuoWeiZhuHeBingShuJu"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
                            return;
                        }
                        //  检查当前拖动数据是否在被合并列表中存在
                    	var rowIds = $("#mergedGridList").jqGrid('getDataIDs');
                    	if (rowIds.length > 0) {
                    		for (var i=0; i<rowIds.length; i++) {
                    			var rowId = rowIds[i];
                    			var rowData = $('#mergedGridList').jqGrid('getRowData', rowId);
                    			if (rowData.prodId == prodId) {
                                    ev.helper.dropped = false;
                                    break;
                                }
                    		}
                    	}
                    } else if (tableId == "mergedGridList") {    //  被合并列表
                    	// 检查当前拖动数据是否在主合并列表中存在
                    	var mainId = $("#mergerGridList").jqGrid('getDataIDs')[0];
                    	var mainRowData = $('#mergerGridList').jqGrid('getRowData', mainId);
                    	if (mainRowData.prodId == prodId) {
                            ev.helper.dropped = false;
                            return;
                        }
                        //  检查当前拖动数据是否在被合并列表中存在
                    	for (var i=0; i<len; i++) {
                    		var rowId = ids[i];
                    		var rowData = $('#mergedGridList').jqGrid('getRowData', rowId);
                    		if (rowData.prodId == prodId) {
                    			ev.helper.dropped = false;
                    			break;
                    		}
                    	}
                    }
                }
            });
            $("#mergerGridList").jqGrid('gridDnD',{connectWith:'#gridlist'});
            $("#mergedGridList").jqGrid('gridDnD',{connectWith:'#gridlist'});
            
        });
        

        //查询
        function gridSerch() {
            var drugcode = $.trim($("#drugcode").val());
            var qualityspecName = $.trim($("#qualityspecName").val());
            var prodCompName = $.trim($("#prodCompName").val());
            var exceptIds = [];
            
            var mainId = $("#mergerGridList").jqGrid('getDataIDs')[0];
            var mainRowData = $('#mergerGridList').jqGrid('getRowData', mainId);
            if (mainRowData.prodId) {
            	exceptIds.push({"prodId" : mainRowData.prodId});
            }
            var ids = $("#mergedGridList").jqGrid('getDataIDs');
            for (var i=0; i<ids.length; i++) {
                var rowId = ids[i];
                var rowData = $('#mergedGridList').jqGrid('getRowData', rowId);
                if (rowData.prodId) {
                	exceptIds.push({"prodId" : rowData.prodId});
                }
            }
            
            $("#gridlist").jqGrid('setGridParam',{  
                datatype:'json',
                postData:{
                	"drugCode" : drugcode,
                    "qualityspecName" : qualityspecName,
                    "prodCompName" : prodCompName,
                    "exceptIds" : json.jsonToString(exceptIds)
                }
            }).trigger("reloadGrid"); //重新载入  
        }
        
        //  保存合并
        function saveMergeDrugInfo() {
        	var mergerProdId = null;
        	var mergedProdIds = [];
        	var data = "";
        	
        	// 获取主合并记录id
        	var rowIds = $("#mergerGridList").jqGrid('getDataIDs');
            if (rowIds.length == 1) {
                var rowId = rowIds[0];
                var rowData = $('#mergerGridList').jqGrid('getRowData', rowId);
                mergerProdId = rowData.prodId || null;
            } else {
            	$.alert("<spring:message code="message.yanZheng.QingXuanZeZhuHeBingShuJu"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
            	return;
            }
            
            //  获取被合并列表id集合
            rowIds = $("#mergedGridList").jqGrid('getDataIDs');
            if (rowIds.length > 0) {
                for (var i=0; i<rowIds.length; i++) {
                    var rowId = rowIds[i];
                    var rowData = $('#mergedGridList').jqGrid('getRowData', rowId);
                    mergedProdIds.push({"prodId" : rowData.prodId || null});
                    data += rowData.qualityspecName+",";
                }
            } else {
            	$.alert("<spring:message code="message.yanZheng.QingXuanZeBeiHeBingShuJu"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
            	return;
            }
            
            data = data.substring(0,data.length-1);
            $.HN.message.confirm("<spring:message code="message.yanZheng.QueDingHeBingYiXiaYaoPinMa"/><spring:message code="message.yanZheng.wenHao"/><br>"+data, '', '').on(function (e) {
                if (e) {
            //  保存
            $.post("${ctx}/stdProdDrugInfo/saveMergeDrugInfo.html", {
            	"mergerProdId" : mergerProdId,
            	"mergedProdIds" : json.jsonToString(mergedProdIds)
            }, function(result) {
            	if (result.success) {
            		$.alert("<spring:message code="message.yanZheng.HeBingSuccess"/>"+mergedProdIds.length+"<spring:message code="message.lable.tiao"/>！", "success", function() {
            			$.HN.window.close("mergeDrugInfo");
            		});
            	} else {
            		$.alert(result.msg||"<spring:message code="message.HN.alert.fail"/>", "error");
            	}
            }, "json");
            
                }
            }); 
        }
    </script>
</body>
</html>








